package com.hyb.controller;


import com.alibaba.fastjson.JSON;
import com.hyb.Util.JwtUtil;
import com.hyb.Util.Md5Util;
import com.hyb.Vo.StaffRoleVo;
import com.hyb.entity.Staff;
import com.hyb.entity.StaffRole;
import com.hyb.mapper.RoleMapper;
import com.hyb.mapper.StaffRoleMapper;
import com.hyb.page.PageReq;
import com.hyb.page.VoQuery;
import com.hyb.response.Result;
import com.hyb.service.PermissionService;
import com.hyb.service.RoleService;
import com.hyb.service.StaffRoleService;
import com.hyb.service.StaffService;
import com.hyb.token.JwtToken;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * <p>
 *  员工Controller
 * </p>
 *
 * @author hyb
 * @since 2021-12-29
 */
@RestController
@RequestMapping("/staff")
public class StaffController {

    @Autowired
    private StaffService staffService;

    @Autowired
    private RoleService roleService;

    @Autowired
    private StaffRoleService staffRoleService;

    @RequestMapping(value = "/login",method = RequestMethod.POST)
    @ApiOperation("登录")
    public String login(@RequestBody Staff staff) {

        if (staff.getStaffPassword() == null||staff.getStaffEmail()==null){
            return JSON.toJSONString(new Result().setCode(500).setMessage("密码或用户不能为空"));
        }
        Subject subject = SecurityUtils.getSubject();
        //获得一个token
        String jwt = JwtUtil.createJWT(staff.getStaffEmail(),"staff","");

        JwtToken jwtToken = new JwtToken(jwt,staff.getStaffPassword());
        try {
            subject.login(jwtToken);
        }catch (UnknownAccountException e){
            return JSON.toJSONString(new Result().setCode(401).setMessage("账号不存在"));
        }catch (IncorrectCredentialsException e){
            return JSON.toJSONString(new Result().setCode(401).setMessage("密码错误"));
        }
        Staff backStaff = staffService.getStaffBuyStaffEmail(staff.getStaffEmail());
        Set<String> roles = roleService.getRolesByStaffEmail(staff.getStaffEmail());
        backStaff.setStaffPassword(null);
        backStaff.setStaffSalt(null);
        Map<String,Object> map = new HashMap<>();
        map.put("staff",backStaff);
        map.put("token",jwt);
        map.put("roles",roles);
        return JSON.toJSONString(new Result().setCode(200).setMessage("登录成功").setData(map));
    }

    @RequestMapping(value = "/staffInfo",method = RequestMethod.PUT)
    @ApiOperation("修改我的信息")
    public String updateMyStaffInfo(@RequestBody Staff staff){
        staffService.updateStaff(staff);
        return JSON.toJSONString(new Result().setCode(200).setMessage("修改成功"));
    }


    @RequestMapping(value = "/getMemuList" , method = RequestMethod.GET)
    @ApiOperation("获取侧边栏")
    public String getMenuList(String staffEmail){
        if (StringUtils.isEmpty(staffEmail)){
            return JSON.toJSONString(new Result().setCode(401).setMessage("未登录"));
        }

        List<Map<String, Object>> memuList = staffService.getMemuListByStaffEmail(staffEmail);
        System.out.println(staffEmail);
        return JSON.toJSONString(new Result().setCode(200).setMessage("获取成功").setData(memuList));
    }


    @RequestMapping(value = "/allStaffsByVo",method = RequestMethod.GET)
    @ApiOperation("获取条件查询的员工信息")
//    @RequiresPermissions("data:staff_list")
    public String getAllStaffByVo(Integer limit, Integer current, String staffName, String role){
        PageReq staffsByWarpper = staffService.getStaffsByWarpper(current, limit, staffName, role);
        return JSON.toJSONString(new Result().setCode(200).setMessage("加载成功").setData(staffsByWarpper));
    }

    @RequestMapping(value = "/allStaff",method = RequestMethod.GET)
    @ApiOperation("获取所有员工信息")
//    @RequiresPermissions("data:staff_list")
    public String getAllStaff(Integer limit,Integer current){

        PageReq staffsByWarpper = staffService.getStaffsByWarpper(current, limit, null, null);
        return JSON.toJSONString(new Result().setCode(200).setMessage("加载成功").setData(staffsByWarpper));
    }

    @RequestMapping(value = "/createStaff",method = RequestMethod.POST)
    @ApiOperation("新建员工信息")
//    @RequiresPermissions("data:staff_create")
    public String createstaffInfo(@RequestBody StaffRoleVo staffRoleVo){

        staffService.createStaff(staffRoleVo);
        Staff staff = staffService.getStaffBuyStaffEmail(staffRoleVo.getStaffEmail());
        StaffRole staffRole = new StaffRole();
        staffRole.setStaffId(staff.getStaffId());
        staffRole.setRoleId(staffRoleVo.getRoleId());
        staffRoleService.createStaffRoles(staffRole);
        return JSON.toJSONString(new Result().setCode(200).setMessage("保存成功"));
    }

    @RequestMapping(value = "/loadStaff",method = RequestMethod.GET)
    @ApiOperation("加载员工信息")
    public String loadStaff(@RequestParam Integer staffId){
        StaffRoleVo staffAndRoleBystaffId = staffService.getStaffAndRoleBystaffId(staffId);
        return JSON.toJSONString(new Result().setCode(200).setMessage("加载成功").setData(staffAndRoleBystaffId));
    }

    @RequestMapping(value = "/updateStaff",method = RequestMethod.PUT)
    @ApiOperation("修改员工信息")
    public String updateStaff(@RequestBody StaffRoleVo staffRoleVo){
        staffService.updateStaff(staffRoleVo);
        staffRoleService.updateStaffRoles(staffRoleVo);
        return JSON.toJSONString(new Result().setCode(200).setMessage("修改成功"));
    }

    @RequestMapping(value = "/loadAllAccountManager",method = RequestMethod.GET)
    @ApiOperation("获取所有客户经理的信息")
    public String loadAllAccountManager(){
        List<Staff> allAccountManager = staffService.getAllAccountManager();
        return JSON.toJSONString(new Result().setCode(200).setMessage("加载成功").setData(allAccountManager));
    }
}
